<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table,td{
            border-collapse: collapse;
        }

        td{
            padding: 10px 20px;
        }
    </style>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">

        //创建虚拟 DOM 
        class App extends React.Component{
            
            state = {
                heros : [
                    {id:1, name: '孙膑', type: '辅助'},
                    {id:2, name: '小乔', type: '中单'},
                    {id:3, name: '扁鹊', type: '法师'}
                ]
            }

            render(){
                return <div>
                    名称:<input type="text" ref={el => this.name = el}/> <br/>
                    类型:<input type="text" ref={el => this.type = el}   /> <br/>
                    <button onClick={this.getFormData}>添加</button>
                    <hr/>
                    <table border="1">
                        <thead>
                            <tr><td>ID</td><td>名字</td><td>类型</td></tr>    
                        </thead>
                        <tbody>
                            {
                                this.state.heros.map((item, index) => {
                                    return <tr key={index}><td>{item.id}</td><td>{item.name}</td><td>{item.type}</td></tr>
                                })
                            }
                        </tbody>
                    </table>

                    </div>
            }

            getFormData = () => {
                //获取名字
                let name = this.name.value;
                let type = this.type.value;
                //添加
                this.setState({
                    heros: [
                        {
                            id: this.state.heros.length+1,
                            name: name,
                            type: type
                        },
                        ...this.state.heros
                    ]
                })
            }
        }

      
        
        //渲染 Image 组件
        ReactDOM.render(<App />, document.querySelector("#root"));


    </script>


</body>
</html> 